//package com.tan.lighting.init;
//
//
//import com.tan.lighting.disruptor.annotation.Disruptor;
//import com.tan.lighting.disruptor.proxy.cglib.DisruptorAdapterServiceCglibProxy;
//import com.tan.lighting.disruptor.proxy.jdk.DisruptorAdapterServiceJdkProxy;
//import com.tan.framework.disruptor.event.CommandEvent;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.BeanInitializationException;
//import org.springframework.beans.factory.config.BeanPostProcessor;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.ApplicationContextAware;
//import org.springframework.core.Ordered;
//import org.springframework.stereotype.Component;
//
//import java.lang.reflect.Field;
//
///**
// * Description:
// * User: Administrator
// * DateTime: 2017/6/13 0013 21:51
// */
//@Component
//public class BeanPostProcesserImpl implements BeanPostProcessor, ApplicationContextAware, Ordered {
//
//    private ApplicationContext applicationContext;
//
//    @Override
//    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//        //System.out.println("before " + beanName);
//        return bean;
//    }
//
//    @Override
//    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//        Class<?> clazz = bean.getClass();
//        Field[] fields = clazz.getDeclaredFields();
//        for (Field field : fields) {
//            //获取Disruptor注解
//            Disruptor disruptor = field.getAnnotation(Disruptor.class);
//            if (disruptor != null) {
//                field.setAccessible(true);
//                if (!field.getType().isInterface()) {
//                    throw new BeanInitializationException("Disruptor annotation only support interface");
//                }
//                //获取disruptor实例
//                Object disruptorObj = applicationContext.getBean(disruptor.value());
//                if (disruptorObj == null) {
//                    throw new BeanInitializationException("Disruptor bean name not found in bean");
//                }
//                try {
//                    String targetName = field.getName();
//                    if (!disruptor.name().equals("")) {
//                        targetName = disruptor.name();
//                    }
//
//                    //获取需要经过disruptor的 目标对象。 一般都是service实例
//                    Object target = applicationContext.getBean(targetName);
//                    if (target == null) {
//                        throw new BeanInitializationException("Disruptor target bean name not found in bean");
//                    }
//                    //创建代理对象
//                    String proxyType = disruptor.proxyType();
//                    Object proxy = null;
//                    if (proxyType.equals("") || proxyType.equals("jdk")) {
//                        DisruptorAdapterServiceJdkProxy handler = new DisruptorAdapterServiceJdkProxy(target,
//                                (com.lmax.disruptor.dsl.Disruptor<CommandEvent>) disruptorObj);
//                        proxy = handler.getProxy(bean.getClass().getClassLoader());
//                    } else if (proxyType.equals("cglib")) {
//                        DisruptorAdapterServiceCglibProxy cglibProxy = new DisruptorAdapterServiceCglibProxy(target, (com.lmax.disruptor.dsl.Disruptor<CommandEvent>) disruptorObj);
//                        proxy = cglibProxy.getProxy();
//                    } else {
//                        throw new BeanInitializationException("proxyType error");
//                    }
//                    field.set(bean, field.getType().cast(proxy));
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return bean;
//    }
//
//    @Override
//    public int getOrder() {
//        return LOWEST_PRECEDENCE;
//    }
//
//    @Override
//    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//        this.applicationContext = applicationContext;
//    }
//}
